<template>
  <view class="login-container">
    <!-- Logo区域 -->
    <view class="logo-area">
      <image class="logo" src="/static/logo.png" mode="aspectFit" />
    </view>

    <!-- 登录方式切换 -->
    <view class="login-tabs">
      <view 
        class="tab-item"
        :class="{ active: currentTab === 0 }"
        @click="currentTab = 0"
      >
        密码登录
      </view>
      <view 
        class="tab-item"
        :class="{ active: currentTab === 1 }"
        @click="currentTab = 1"
      >
        验证码登录
      </view>
    </view>

    <!-- 密码登录 -->
    <view v-if="currentTab === 0" class="login-form">
      <view class="input-item">
        <image class="icon" src="/static/icons/phone.svg" />
        <input 
          v-model="phone"
          type="number"
          placeholder="请输入手机号"
          maxlength="11"
        />
      </view>
      <view class="input-item">
        <image class="icon" src="/static/icons/lock.png" />
        <input 
          v-model="password"
          type="password"
          placeholder="请输入密码"
        />
      </view>
      
      <view class="action-row">
        <text class="forget-pwd" @click="goForgetPwd">忘记密码?</text>
        <text class="register" @click="goRegister">注册账号</text>
      </view>

      <button class="login-btn" @click="handlePhoneLogin">登录</button>

      <view class="other-login">
        <view class="divider">
          <text>其他登录方式</text>
        </view>
        <view class="social-login">
          <image class="wechat-icon" src="/static/icons/wechat.png" @click="handleWechatLogin" />
        </view>
      </view>
    </view>

    <!-- 验证码登录 -->
    <view v-if="currentTab === 1" class="login-form">
      <view class="input-item">
        <image class="icon" src="/static/icons/phone.svg" />
        <input 
          v-model="smsForm.phone"
          type="number"
          placeholder="请输入手机号"
          maxlength="11"
        />
      </view>
      <view class="input-item code-input">
        <image class="icon" src="/static/icons/lock.png" />
        <input 
          v-model="smsForm.code"
          type="number"
          placeholder="请输入验证码"
          maxlength="6"
        />
        <text 
          class="send-code-btn" 
          :class="{ disabled: counting > 0 }"
          @click="handleSendCode"
        >
          {{ counting > 0 ? `${counting}s后重新获取` : '获取验证码' }}
        </text>
      </view>
      
      <view class="action-row">
        <text class="register" @click="goRegister">注册账号</text>
      </view>

      <button class="login-btn" @click="handleSmsLogin">登录</button>

      <view class="other-login">
        <view class="divider">
          <text>其他登录方式</text>
        </view>
        <view class="social-login">
          <image class="wechat-icon" src="/static/icons/wechat.png" @click="handleWechatLogin" />
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const currentTab = ref(0)
const phone = ref('')
const password = ref('')
const counting = ref(0)
const timer = ref(null)

const smsForm = ref({
  phone: '',
  code: ''
})

// 处理发送验证码
const handleSendCode = async () => {
  if (counting.value > 0) return
  if (!smsForm.value.phone) {
    uni.showToast({
      title: '请输入手机号',
      icon: 'none'
    })
    return
  }
  
  try {
    // 直接发送短信验证码
    // await sendSmsCode()
    startCountDown()
    
    uni.showToast({
      title: '验证码已发送',
      icon: 'success'
    })
  } catch (error) {
    uni.showToast({
      title: '发送失败，请重试',
      icon: 'none'
    })
  }
}

// 开始倒计时
const startCountDown = () => {
  counting.value = 60
  timer.value = setInterval(() => {
    if (counting.value > 0) {
      counting.value--
    } else {
      clearInterval(timer.value)
    }
  }, 1000)
}

// 验证码登录
const handleSmsLogin = () => {
  if (!smsForm.value.phone || !smsForm.value.code) {
    uni.showToast({
      title: '请输入手机号和验证码',
      icon: 'none'
    })
    return
  }
  // 处理验证码登录逻辑
}

const handlePhoneLogin = () => {
  // 处理账号密码登录逻辑
}

const handleWechatLogin = () => {
  // 处理微信登录逻辑
}

const goRegister = () => {
  uni.navigateTo({
    url: '/pages/register/index'
  })
}

const goForgetPwd = () => {
  uni.navigateTo({
    url: '/pages/forget-password/index'
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  padding: 0 40rpx;
  min-height: 100vh;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;

  .login-tabs {
    display: flex;
    width: 100%;
    border-bottom: 1px solid #eee;
    margin-bottom: 40rpx;

    .tab-item {
      flex: 1;
      text-align: center;
      padding: 20rpx 0;
      font-size: 28rpx;
      color: #999;
      position: relative;

      &.active {
        color: #333;
        font-weight: 500;
        
        &::after {
          content: '';
          position: absolute;
          bottom: -2rpx;
          left: 50%;
          transform: translateX(-50%);
          width: 40rpx;
          height: 4rpx;
          background-color: #000;
          border-radius: 2rpx;
        }
      }
    }
  }

  .login-form {
    width: 100%;

    .input-item {
      display: flex;
      align-items: center;
      padding: 24rpx 30rpx;
      margin-bottom: 20rpx;
      background-color: #f8f8f8;
      border: 1px solid #eeeeee;
      border-radius: 45rpx;
      
      .icon {
        width: 40rpx;
        height: 40rpx;
        margin-right: 20rpx;
      }

      input {
        flex: 1;
        font-size: 28rpx;
        color: #333;
        background: transparent;
        height: 44rpx;
        
        &::placeholder {
          color: #999;
        }
      }
    }

    .input-item {
      border-bottom: none;
    }

    .input-item:focus-within {
      border-color: #e0e0e0;
      background-color: #f5f5f5;
    }

    .action-row {
      display: flex;
      justify-content: space-between;
      margin: 30rpx 0;
      
      .forget-pwd {
        color: #999;
        font-size: 26rpx;
      }
      
      .register {
        color: #333;
        font-size: 26rpx;
        font-weight: 500;
      }
    }

    .login-btn {
      width: 100%;
      height: 90rpx;
      line-height: 90rpx;
      text-align: center;
      background-color: #000;
      color: #fff;
      border-radius: 45rpx;
      font-size: 32rpx;
      margin: 40rpx 0;
    }

    .other-login {
      margin-top: 60rpx;

      .divider {
        position: relative;
        text-align: center;
        margin-bottom: 40rpx;

        &::before,
        &::after {
          content: '';
          position: absolute;
          top: 50%;
          width: 30%;
          height: 1px;
          background-color: #eee;
        }

        &::before {
          left: 0;
        }

        &::after {
          right: 0;
        }

        text {
          color: #999;
          font-size: 26rpx;
          padding: 0 20rpx;
          background-color: #fff;
        }
      }

      .social-login {
        display: flex;
        justify-content: center;

        .wechat-icon {
          width: 80rpx;
          height: 80rpx;
        }
      }
    }

    .agreement {
      text-align: center;
      font-size: 24rpx;
      margin-top: 40rpx;

      .agreement-text {
        color: #999;
      }

      .link {
        color: #333;
        margin: 0 4rpx;
      }
    }

    .code-input {
      position: relative;
      
      .send-code-btn {
        position: absolute;
        right: 30rpx;
        color: #333;
        font-size: 26rpx;
        padding: 4rpx 0;
        
        &.disabled {
          color: #999;
        }
      }
    }
  }
}

.captcha-popup {
  width: 600rpx;
  background: #fff;
  border-radius: 16rpx;
  overflow: hidden;

  .captcha-title {
    padding: 30rpx;
    text-align: center;
    font-size: 32rpx;
    font-weight: 500;
    color: #333;
  }

  .captcha-content {
    padding: 30rpx;

    .captcha-image {
      width: 100%;
      height: 200rpx;
      background: #f8f8f8;
      margin-bottom: 30rpx;
      border-radius: 8rpx;
    }

    .captcha-input-wrap {
      .captcha-input {
        width: 100%;
        height: 90rpx;
        background: #f8f8f8;
        border-radius: 45rpx;
        padding: 0 30rpx;
        font-size: 28rpx;
        border: 1px solid #eeeeee;
      }
    }
  }

  .captcha-footer {
    display: flex;
    border-top: 1px solid #eee;
    margin-top: 20rpx;
    
    text {
      flex: 1;
      text-align: center;
      padding: 25rpx 0;
      font-size: 30rpx;
      
      &.cancel-btn {
        color: #999;
        border-right: 1px solid #eee;
      }
      
      &.confirm-btn {
        color: #000;
        font-weight: 500;
      }
    }
  }
}

.logo-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 100rpx;
  margin-bottom: 60rpx;

  .logo {
    width: 160rpx;
    height: 160rpx;
    margin-bottom: 20rpx;
  }

  .title {
    font-size: 36rpx;
    font-weight: 600;
    color: #333;
  }
}
</style>
